<template>
    <div class="wheel-tabs">
        <div class="wheel-tabs-nav" ref="container">
            <div class="wheel-tabs-nav-item" 
                v-for="(t, index) in titles" 
                :key="index"
                :ref="el => { if (t === selected) selectedItem = el }"
                :class="{selected: t === selected}"
                @click="select(t)"> {{ t }} </div>

            <div class="wheel-tabs-nav-indicator" ref="indicator"></div>
        </div>

        <div class="wheel-tabs-content">
            <component :is="current" :key="current.props.title" />
        </div>
    </div>
</template>

<script lang="ts">

    import { computed, ref, onMounted, watchEffect, onUpdated } from 'vue';
    import Tab from './Tab.vue'

    export default {
        props: {
            selected: {
                type: String
            }
        },
        setup(props, context) {
            const selectedItem = ref<HTMLDivElement>(null);
            const indicator = ref<HTMLDivElement>(null);
            const container = ref<HTMLDivElement>(null);

            onMounted(() => {
                watchEffect(() => {
                    const { width, left: left2 } = selectedItem.value.getBoundingClientRect();
                    indicator.value.style.width = width + 'px';

                    const { left: left1 } = container.value.getBoundingClientRect();
                    const left = left2 - left1;
                    indicator.value.style.left = left + 'px';
                }, {flush: 'post'})
            });

            const defaults = context.slots.default();
            defaults.forEach((tag) => {
                // @ts-ignore
                if( tag.type.name !== Tab.name) {
                    throw new Error('Tabs 子标签必须是 Tab');
                }
            });
            
            const titles = defaults.map((tag) => {
                return tag.props.title
            });

            const current = computed(() => {
                return defaults.find(tag => tag.props.title === props.selected)
            });

            const select = ((title: string) => {
                context.emit('update:selected', title);
            });

            return { 
                current, 
                selectedItem, 
                indicator, 
                container, 
                defaults, 
                titles, 
                select 
            }
        }
    }
</script>

<style lang="scss">
    $blue: #40A9FF;
    $color: #333;
    $border-color: #D9D9D9;

    .wheel-tabs {

        &-nav {
            position: relative;
            display: flex;
            color: $color;
            border-bottom: 1px solid $border-color;

            &-item {
                padding: 8px 0;
                margin: 0 16px;
                cursor: pointer;

                &:first-child {
                    margin-left: 0;
                }

                &.selected {
                    color: $blue;
                }
            }

            &-indicator {
                position: absolute;
                left: 0;
                bottom: -1px;
                width: 100px;
                height: 3px;
                background: $blue;
                transition: all 250ms;
            }
        }

        &-content {
            padding: 8px 0;
        }
    }
</style>